//
//  AyProfileHeaderView.m
//  xWeibo
//
//  Created by alvis mac on 11/5/15.
//  Copyright © 2015 alvis. All rights reserved.
//

#import "AyProfileHeaderView.h"
#import "AyNumberBtn.h"
#import "AyIconView.h"
#import "UIImageExtend.h"
#import "AyUserTool.h"
#import "AyStatusSendController.h"
#import "AyNavigationController.h"
#import "AyTableBarController.h"
#import "AyNavigationController.h"
#import "IWFriendsViewController.h"
#import "IWFollowersViewController.h"

@interface AyProfileHeaderView()

@property (weak, nonatomic) IBOutlet UIButton *detailBtn;
@property (weak, nonatomic) IBOutlet AyIconView *iconView;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *dividers;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *sexView;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet AyNumberBtn *statusCount;
@property (weak, nonatomic) IBOutlet AyNumberBtn *friendsCount;
@property (weak, nonatomic) IBOutlet AyNumberBtn *fansCount;
@property (weak, nonatomic) IBOutlet AyNumberBtn *likeCount;

@end

@implementation AyProfileHeaderView


+ (instancetype)header{
    return [[[NSBundle mainBundle] loadNibNamed:@"AyProfileHeaderView" owner:nil options:nil] firstObject];
}


- (void)awakeFromNib{

    self.autoresizingMask = UIViewAutoresizingNone;
    
    // 1.详情资料
    [_detailBtn setBackgroundImage:[UIImage resizedImageWithName:@"userinfo_relationship_button_background"] forState:UIControlStateNormal];
    [_detailBtn setBackgroundImage:[UIImage resizedImageWithName:@"userinfo_relationship_button_highlighted"] forState:UIControlStateHighlighted];
    
    // 2.分隔线
    for (UIView *divider in _dividers) {
        CGRect frame = divider.frame;
        if (frame.size.width > 20) { // 横线
            frame.size.height = 0.1;
        } else { // 竖线
            frame.size.width = 0.1;
        }
        divider.alpha = 0.5;
        divider.frame = frame;
    }
    
    // 3.阴影
    self.layer.shadowColor = [UIColor grayColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 0.5);
    self.layer.shadowOpacity = 0.7;
    
    // 4.默认值
    _statusCount.number = 0;
    _friendsCount.number = 0;
    _fansCount.number = 0;
    _likeCount.number = 0;

}


- (void)setParam:(AyUserInfoParam *)param{
    _param = param;
    [AyUserTool userInfoWithParam:param success:^(AyUserInfoResult *user) {
        
        // 3.1.设置头像
        [_iconView setUser:user iconType:IWIconTypeBig];
        
        // 3.2.设置数量
        _statusCount.number = user.statuses_count;
        _friendsCount.number = user.friends_count;
        _fansCount.number = user.followers_count;
        
        // 3.3.描述
        if (user.verified_reason.length) {
            _descLabel.text = [NSString stringWithFormat:@"新浪认证:%@", user.verified_reason];
        } else if (user.desc.length) {
            _descLabel.text = user.desc;
        } else {
            _descLabel.text = @"这个人很懒，什么也没写！";
        }
        
        // 3.4.昵称
        _nameLabel.text = user.name;
        CGFloat nameMaxW = self.frame.size.width - _nameLabel.frame.origin.x - 30;
        CGSize nameSize = [user.name sizeWithFont:_nameLabel.font constrainedToSize:CGSizeMake(nameMaxW, MAXFLOAT)];
        CGRect nameF = _nameLabel.frame;
        CGFloat oldW = nameF.size.width;
        nameF.size.width = nameSize.width;
        _nameLabel.frame = nameF;
        
        // 3.5.性别
        CGRect sexF = _sexView.frame;
        sexF.origin.x += nameSize.width - oldW;
        _sexView.frame = sexF;
        
    } failure:^(NSError *error) {
 
    }];
    
}


- (IBAction)statusClick {
    AyTableBarController *root = (AyTableBarController *)self.window.rootViewController;
    AyNavigationController *nav = (AyNavigationController *)root.selectedViewController;
    AyStatusSendController *sl = [[AyStatusSendController alloc] init];
    [nav pushViewController:sl animated:YES];
}

- (IBAction)friendsClick {
    UITabBarController *root = (UITabBarController *)self.window.rootViewController;
    UINavigationController *nav = (UINavigationController *)root.selectedViewController;
    IWFriendsViewController *vc = [[IWFriendsViewController alloc] init];
    [nav pushViewController:vc animated:YES];
}

- (IBAction)fansClick {
    UITabBarController *root = (UITabBarController *)self.window.rootViewController;
    UINavigationController *nav = (UINavigationController *)root.selectedViewController;
    IWFollowersViewController *vc = [[IWFollowersViewController alloc] init];
    [nav pushViewController:vc animated:YES];
}

@end
